package com.projectx.action.register;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputLayout;
import com.google.gson.Gson;
import com.projectx.R;
import com.projectx.action.login.LoginActivity;
import com.projectx.entity.User;
import com.projectx.util.ActivityManage;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class PersonDataActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_data);
        ActivityManage.addActivity("Person_dataActivity", this);
        final TextInputLayout textInputLayout_name = findViewById(R.id.textinput_name);
        final TextInputLayout textInputLayout_id = findViewById(R.id.textinput_id);
        final TextInputLayout textInputLayout_email = findViewById(R.id.textinput_email);
        final TextInputLayout textInputLayout_ipone = findViewById(R.id.textinput_ipone);
        final TextInputLayout textInputLayout_qq = findViewById(R.id.textinput_qq);
        final EditText name = findViewById(R.id.name);
        final EditText id = findViewById(R.id.id);
        final EditText email = findViewById(R.id.email);
        final EditText ipone = findViewById(R.id.ipone);
        final EditText qq = findViewById(R.id.qq);
        final TextView text = findViewById(R.id.text);
        final Button dialog = findViewById(R.id.dialog);
        final Button register = findViewById(R.id.Button_register);
        final Button back = findViewById(R.id.Button_back);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long getQQ, getId, getIpone;
                final String Username = getIntent().getStringExtra("username");
                final String Password = getIntent().getStringExtra("password");
                final String getname = name.getText().toString();
                final String getemail = email.getText().toString();
                final String getipone = ipone.getText().toString();
                final String getqq = qq.getText().toString();
                boolean flag = true;
                if (!getipone.equals("")) {
                    boolean flag1 = true;
                    if (getipone.length() >= 16) {
                        textInputLayout_ipone.setError("手机号码过长");
                        flag1 = false;
                        flag = false;
                    }
                    if (flag1 == true) {
                        try {
                            getIpone = Long.parseLong(getipone);
                        } catch (NumberFormatException e) {
                            flag = false;
                            textInputLayout_ipone.setError("手机号码应有数字组成");
                        }
                    }
                }
                if (!getqq.equals("")) {
                    boolean flag1 = true;
                    if (getqq.length() >= 16) {
                        textInputLayout_qq.setError("qq号码过长");
                        flag = false;
                        flag1 = false;
                    }
                    if (flag1 == true) {
                        try {
                            getQQ = Long.parseLong(getqq);
                        } catch (NumberFormatException e) {
                            flag = false;
                            textInputLayout_qq.setError("qq号码应由数字组成");
                        }
                    }
                }
                if (!id.getText().toString().equals("")) {
                    boolean flag1 = true;
                    if (id.getText().toString().length() >= 16) {
                        textInputLayout_id.setError("学号过长");
                        flag = false;
                        flag1 = false;
                    }
                    if (flag1 == true) {
                        try {
                            getId = Long.parseLong(id.getText().toString());
                        } catch (NumberFormatException e) {
                            flag = false;
                            textInputLayout_id.setError("学号应由数字组成");
                        }
                    }
                } else {
                    textInputLayout_id.setError("请填写学号");
                }
                if (getname.equals("")) {
                    flag = false;
                    textInputLayout_name.setError("请输入真实姓名");
                }
                if (text.getText().equals("选择性别")) {
                    text.setText("请选择性别");
                    text.setTextColor(0xFFFF0000);
                    flag = false;
                }
                Log.d("lzw", flag + "");
                if (flag == true) {
                    textInputLayout_qq.setError(null);
                    textInputLayout_name.setError(null);
                    textInputLayout_id.setError(null);
                    FutureTask<JSONObject> task = new FutureTask<JSONObject>(new Callable<JSONObject>() {
                        @Override
                        public JSONObject call() throws Exception {
                            JSONObject getMessage = null;
                            try {
                                Socket socket = new Socket("106.15.192.117", 2333);
                                Gson gson = new Gson();
                                User user = new User();
                                user.setFlag(2);
                                user.setUserName(Username);
                                user.setPassword(Password);
                                user.setRealName(getname);
                                if (!id.getText().toString().equals("")) {
                                    user.setId(Long.parseLong(id.getText().toString()));
                                }
                                user.setMail(getemail);
                                if (!ipone.getText().toString().equals("")) {
                                    user.setPhone(Long.parseLong(ipone.getText().toString()));
                                }
                                if (!qq.getText().toString().equals("")) {
                                    user.setQq(Long.parseLong(qq.getText().toString()));
                                }
                                if (text.getText().equals("男"))
                                    user.setSex(1);
                                else if (text.getText().equals("女"))
                                    user.setSex(0);
                                else
                                    user.setSex(2);//加了一种未知
                                String message = gson.toJson(user);
                                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                                dataOutputStream.writeUTF(message);
                                dataOutputStream.flush();
                                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                                String netStr = dataInputStream.readUTF();
                                try {
                                    getMessage = new JSONObject(netStr);
                                } catch (JSONException e) {
                                    Log.d("kmh", "doInBackground: 后端JSON字符串有问题祭天吧");
                                } finally {
                                    dataInputStream.close();
                                    dataOutputStream.close();
                                    socket.close();
                                }

                            } catch (IOException ioe) {
                                ioe.printStackTrace();
                            }
                            return getMessage;
                        }
                    });
                    Thread thread = new Thread(task);
                    thread.start();
                    //解析验证JSON字符串返回数据
                    try {
                        JSONObject message = task.get(5, TimeUnit.SECONDS);
                        int code = message.getInt("code");
                        Log.d("Person_dataActivity", message.toString());
                        // progressBar.setVisibility(View.INVISIBLE);
                        if (code == -1) {
                            Toast.makeText(PersonDataActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
                        } else if (code == 0) {
                            Toast.makeText(PersonDataActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(PersonDataActivity.this, LoginActivity.class);
                            startActivity(intent);
                            ActivityManage.exitActivity("Register1");
                            ActivityManage.exitActivity("Person_dataActivity");
                        }
                    } catch (TimeoutException te) {
                        Toast.makeText(PersonDataActivity.this, "时间戳错误", Toast.LENGTH_SHORT).show();
                    } catch (JSONException je) {
                        Toast.makeText(PersonDataActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
                        je.printStackTrace();
                    } catch (Exception ne) {
                        ne.printStackTrace();
                        Toast.makeText(PersonDataActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                    } finally {
                        //progressBar.setVisibility(View.INVISIBLE);
                    }
                }
            }
        });
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Register1", "弹出框呢");
                LayoutInflater inflater = LayoutInflater.from(v.getContext());
                View view = LayoutInflater.from(v.getContext()).inflate(R.layout.sex_choose, null, false);
                final PopupWindow popupWindow = new PopupWindow(view, view.getWidth(), view.getHeight());
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setOutsideTouchable(true);
                // popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
                popupWindow.setBackgroundDrawable(v.getContext().getDrawable(R.drawable.bg_shadow_white));
                popupWindow.setContentView(view);
                popupWindow.showAsDropDown(v, Gravity.CENTER, 0, 0);
                view.findViewById(R.id.man).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        text.setText("男性");
                        text.setTextColor(0xFF000000);
                        popupWindow.dismiss();
                    }
                });
                view.findViewById(R.id.woman).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        text.setText("女性");
                        text.setTextColor(0xFF000000);
                        popupWindow.dismiss();
                    }
                });
                view.findViewById(R.id.personal).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        text.setText("私密");
                        text.setTextColor(0xFF000000);
                        popupWindow.dismiss();
                    }
                });

            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManage.exitActivity("Person_dataActivity");
            }
        });
    }
}

